package com.google.code.jmarias.server.tcp;

import com.google.code.jmarias.common.CardDeck;
import com.google.code.jmarias.common.NewTable;
import com.google.code.jmarias.common.action.SignedAction;
import com.google.code.jmarias.server.phases.NewAbstractPhase;

/**
 * Potomci teto abstraktni tridy obsahuji pravidla hry. Trida je vlaknove zabezpecena
 * (mohou k ni pristupovat ruzna vlakna).
 * @author Ondrej Michal
 */
public abstract class AbstractGame {

    protected abstract NewAbstractPhase getStartingPhase();

    private NewAbstractPhase currentPhase;

    private NewTable table;

    private int id;

    /** Priznak rika, jestli uz hra skoncila. */
    private boolean gameEnd;

    public AbstractGame(int id, NewTable table) {
        this.id = id;
        this.table = table;

        if (table.getDeck() == null) {
            table.setDeck(new CardDeck());
        }

        updateCurrentPhase(getStartingPhase());
        processAction(null);
    }

    public void processAction(SignedAction action) {
        updateCurrentPhase(currentPhase.processAction(action));
    }

    protected void updateCurrentPhase(NewAbstractPhase newPhase) {
        newPhase.setTable(table);
        currentPhase = newPhase;
    }

    public void finishGame() {
        gameEnd = true;
    }

    public boolean isGameEnd() {
        return gameEnd;
    }

    public int getId() {
        return id;
    }

    public NewTable getTable() {
        return table;
    }

}
